package marble.model {

	import flash.display.DisplayObject;
	
	import marble.Facade;
	
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.controls.Spacer;
	import mx.controls.Text;
	import mx.events.ResizeEvent;
	import mx.formatters.DateFormatter;

	public class InfoPopupClass extends VBox {

		private static const MAX_WIDTH:int = 245;

		[Bindable]
		private var facade:Facade = Facade.getInstance();

		public var _elem:Element;
		
		public function InfoPopupClass() {
			addEventListener(ResizeEvent.RESIZE,
				function (event:ResizeEvent):void {
					var diff:int = parent.height - (y + height);
					if (diff < 0) {
						y += diff;
					}
				}
			);
		}
		
		public function get elem():Element {
			return _elem;
		}
		
		[Bindable]
		public function set elem(e:Element):void {

			_elem = e;

			// remove old labels
			for each (var widget:DisplayObject in getChildren()) {
				if (widget is Label) {
					removeChild(widget);
				}
			}

			var lbl:Text = null;

			// session
			if (e.is_session) {
				lbl = new Text();
				lbl.width = MAX_WIDTH;
				lbl.setStyle("fontWeight", "bold");
				lbl.setStyle("fontFamily", "Tahoma");
				lbl.htmlText = "* " + facade.locale.translate('Session') + " *";
				addChild(lbl);
			}

			// date & time
			lbl = new Text();
			lbl.width = MAX_WIDTH;
			lbl.setStyle("fontFamily", "Tahoma");
			lbl.htmlText = "<b>" + facade.locale.translate('Date') + "</b>: "
			+ formatDate(e.getDate()) + ", " + formatTime(e.getDate());
			addChild(lbl);

			// place
			if (e.place != null) {
				lbl = new Text();
				lbl.width = MAX_WIDTH;
				lbl.setStyle("fontFamily", "Tahoma");
				lbl.htmlText = "<b>" + facade.locale.translate('Place') + "</b>: "
				+ e.place.name;
				addChild(lbl);
			}
			
			// duration
			lbl = new Text();
			lbl.width = MAX_WIDTH;
			lbl.setStyle("fontFamily", "Tahoma");
			lbl.htmlText = "<b>" + facade.locale.translate('Duration') + "</b>: "
			+ e.duration + " min";
			addChild(lbl);

			// quantity
			if (e.quantity > 0) {
				lbl = new Text();
				lbl.width = MAX_WIDTH;
				lbl.setStyle("fontFamily", "Tahoma");
				lbl.htmlText = "<b>" + facade.locale.translate('Quantity') + "</b>: "
				+ e.quantity;
				addChild(lbl);
			}

			// price
			if (e.price != null) {
				lbl = new Text();
				lbl.width = MAX_WIDTH;
				lbl.setStyle("fontFamily", "Tahoma");
				lbl.htmlText = "<b>" + facade.locale.translate('Price') + "</b>: "
				+ e.price + " " + (facade.currencySymbol ? facade.currencySymbol : "");
				addChild(lbl);
			}
			
			// description
			if (e.description != null) {

				var sp:Spacer = new Spacer();
				sp.height = 10;
				addChild(sp);

				lbl = new Text();
				lbl.width = MAX_WIDTH;
				lbl.setStyle("fontFamily", "Tahoma");
				lbl.setStyle("fontSize", 10);
				lbl.setStyle("textAlign", "justify");
				lbl.percentHeight = 100;
				lbl.text = e.description;
				addChild(lbl);
			}
		}
		
		private function formatDate(date:Date):String {
			var formatter:DateFormatter = new DateFormatter();
			formatter.formatString = "YYYY-MM-DD";
			return formatter.format(date);
		}

		private function formatTime(date:Date):String {
			var formatter:DateFormatter = new DateFormatter();
			formatter.formatString = "HH:NN";
			return formatter.format(date);
		}
	}
}